#include "SypEngine.h"

#ifdef CHECKMEM
#include "mmgr.h"
#endif
namespace Syp
{
bool EffectRegistry::addEffect(Effect* p)
{

	Effect* old = getEntry(p->getName());
	if(old)
	{//if there is already an similar Effect.
		if(p->getQuality() < old->getQuality())
		{//and p is suckier, we already have a superior effect which works		
			return false;
		}
	}
	//else attempt to add p
	for(PInt i=0;i<p->getNumPasses();++i)
	{
		if(! p->getPass(i)->runnableOnHardware() )
		{
			return false;
		}
	}
	if(old)
	{//new entry has passed runnableOnHardware test, we need to remove the old.
		removeEntry(p->getName());
	}
	addEntry(p->getName(),p);
			return true;
	

}

EffectRegistry* EffectRegistry::pr = 0;

void EffectRegistry::initialise()
{
	if(!pr)
	{
		pr = new EffectRegistry();
	}
};

EffectRegistry& EffectRegistry::getRegistry()
{
	assert(pr && "EffectRegistry has not been intialised.");
	return (*pr);
}

void EffectRegistry::release()
{
	ErrorLogger::getLogger()<<"Releasing EffectRegistry."<<std::endl;	
	if(pr)
	{
		pr->removeAllEntries();
		delete pr;
		pr = 0;
	}
}

void EffectRegistry::dumpInfo()
{
	ErrorLogger::getLogger()<<"EffectRegistry has "<<getNumEntries()<<" Effect(s).\n";
	std::map<String,Effect*>::iterator b = passes.begin();
	std::map<String,Effect*>::iterator e = passes.end();
		while(b!=e)
		{
			ErrorLogger::getLogger()<<b->first<<"  Quality : "<<b->second->getQuality()<<"\n";			
			ErrorLogger::getLogger()<<b->second->getDescription()<<"\n";
			++b;
		}

}
}
